﻿using System.Linq;
using System.Web.Mvc;
using yc.Dal;
using yc.Site.Models;

namespace yc.Site.Controllers
{
	/// <summary>
	/// 用户登录页
	/// </summary>
	public class LoginController : Controller
	{
		public ActionResult Index()
		{
			return View();
		}

		[HttpPost, ActionName("Index")]
		public ActionResult PostIndex(Login model)
		{
			if (ModelState.IsValid)
			{
				using (var db = new DBContext())
				{
					var pwd = MD5.Encrypt(model.Password);

					var user = db.SysUser.FirstOrDefault(p => p.LoginName.Equals(model.LoginName)
						&& p.Password.Equals(pwd));
						//&& p.Available);
					if (user != null) return Redirect(Common.SignIn(user, model.RememberMe));
					ModelState.AddModelError("LoginName", "用户名密码不正确");
				}
			}
			return View(model);
		}
	}
}
